$(function(){

	var $formUpload=$("#form_upload");
	
    /** ************增加文件*************** */
	var $formAdd=$("#form_add");
//    $("#btn_add").click(function(){
//    	$("#modal_add").modal("show");
//    	addTag();
    	addCategory($formAdd);
    	addType();
    	$formAdd.find($("[name=cardinal]")).val(RandomNum(1,100)) ;
//    });
    
    
	function checkAddForm(){
//		if($("#url").val()==""){
//			warnInput($("[name=file]"));
//			return false;
//		}
		if($("[name=name]").val() == ""){
			warnInput($("[name=name]"));
			return false;
		}
		if($("[name=type]").val() == 0){
			warnInput($("[name=type]"));
			return false;
		}
		if($("[name=top]").val() == 0){
			warnInput($("[name=top]"));
			return false;
		}
		if($("[name=second]").val() == 0){
			warnInput($("[name=second]"));
			return false;
		}
		if($("[name=cardinal]").val() == ""){
			warnInput($("[name=cardinal]"));
			return false;
		}
		return true;
	}
	
		/*** 添加论文*/
	 $("#btn_confirm_add").click(function() {
//		 if($("#url").val() == null || $("#url").val() == ""){
//			 alert("请先上传文件");
//			 return false;
//		 }
		 
		if (checkAddForm()) {
//			if($(".tagBox-list ").find("a").length == 0 ){
//				alert("请添加标签");
//				return false;
//			}
//			var labelIds = "";
//			$.each($(".tagBox-list ").find("a"), function(i, k) {
//				labelIds += k.rel + ",";
//			});
//			labelIds = labelIds.substring(0, labelIds.length - 1);
			path = $("#url").val() ;
			size = $formUpload.find("[name=size]").val();
			typeId =$formAdd.find($("[name=type]")).val();
			tcId =$formAdd.find($("[name=top]")).val();
			scId =$formAdd.find($("[name=second]")).val();
			dcId =$formAdd.find($("[name=detail]")).val();
			var price = 0;
			if($formAdd.find($("[name=price]")).val() != ""){
				price = $formAdd.find($("[name=price]")).val();
			}
			introduction = $("textarea[name='content1']").val();
			
			var tag = $("#tags").val();
			var reference = $("#reference").val();
			
			var source = $formAdd.find($("[name=source]")).val();
			var author = $formAdd.find($("[name=author]")).val();
			 var status = $("input[name='status']:checked").val();
			$.ajax({ // 此处用同步处理，避免出现val()比reload早的问题
				url : serverURL + "view/thesis/label/addBatch?tags="+tag,
				type : "post",
				async : false,
				success : function(data) {
					if (data.code == 0) {
							$.post(serverURL + "view/thesis/add", {"authStatus":'1',"name" : $("[name=name]").val(),"path" : path,"size" : size,"labelIds" : data.data,"typeId" : typeId, "tcId":tcId,"scId" : scId,"dcId" : dcId,"introduction":introduction,"price":price,"reference":reference,"source":source,"author":author,"status":status}, function(data) {
							if (data.code == 0) {
								alert("添加成功");
								window.location.href="thesis.htm";
							} else if (data.code != -1) {
								alert("添加失败,请稍后重试");
							}
						});
	
					} else if (data.code != -1) {
						alert("数据添加失败");
					}
				}
			});		
			

		}
	})
	
	/** ***********添加分类********************* */
	function addCategory(object) {
			// 一级分类选择
			var $top = object.find("[name=top]"),
			$second = object.find("[name=second]"),
			$detail = object.find("[name=detail]"); 
			
			$.get("view/thesis/category/getTopCategory", function(data) {
				$.each(data.data, function(i, obj) {
					$top.append("<option value='" + obj.tcId + "'>" + obj.tcName+ "</option>");
				});
			}, "json");
			/*--------------修改的一、二、三级二级联动处理----------------*/
			$top.change(function() {
				districtEdit.clearDetail();
				districtEdit.reloadSecond($(this).val());
			});
	
			$second.change(function() {
				districtEdit.reloadDetail($(this).val());
			});
	
			var districtEdit = {
				clearSecond : function() {
					$second.find("option[value!=0]").remove();
				},
				clearDetail : function() {
					$detail.find("option[value!=0]").remove();
				},
				reloadSecond : function(tcId) {
					districtEdit.clearSecond();
					if (tcId != 0) {
						$.ajax({ // 此处用同步处理，避免出现val()比reload早的问题
							url : serverURL + "view/thesis/category/getSecondaryCategory?"+ "tcId=" + tcId,
							type : "get",
							async : false,
							success : function(data) {
								if (data.code == 0) {
									var html = "";
									$.each(data.data, function(i, k) {
										html += "<option value='" + k.scId +"'>"+ k.scName + "</option>";
									});
									$second.append(html);
								} else if (data.code != -1) {
									alert("一二级分类数据加载失败");
								}
							}
						});
					}
				},
				reloadDetail : function(scId) {
					districtEdit.clearDetail();
					if (scId != 0) {
						$.ajax({
							url : serverURL + "view/thesis/category/getDetailCategory?scId=" + scId+"&dcTypeId=0",
							type : "get",
							async : false,
							success : function(data) {
								if (data.code == 0) {
									$.each(data.data, function(i, k) {
										 $detail.append("<option value='" + k.dcId + "'>"+ k.dcName + "</option>");
									});
								} else if (data.code != -1) {
									alert("三级分类数据加载失败");
								}
							}
						});
					}
				}
			}; 
		}
	
	/***********添加分类****************/
	function addType(){
		$.get("type/list", function(data) {	
			$formAdd.find("[name=type]").empty();
			$.each(data.data, function(i, obj) {
			$formAdd.find("[name=type]").append("<option value='" + obj.typeId + "'>" + obj.typeName+ "</option>");	
			});
		}, "json");
	}
	
    $("#tags").tagsInput({
        width: 'auto'
    });
    
    $("#reference").tagsInput({
        width: 'auto'
    });
	/****生成随机数min ≤ r ≤ max***/
	function RandomNum(Min,Max){
	      var Range = Max - Min;
	      var Rand = Math.random();
	      var num = Min + Math.round(Rand * Range); //四舍五入
	      return num;
	}
});